﻿using System;
using System.Collections.Generic;
using System.Linq;
using ReLogic.Utilities;
using Terraria.Chat.Commands;
using Terraria.Localization;
using Terraria.ModLoader;

namespace Terraria.Chat
{
	// Token: 0x0200048E RID: 1166
	public class ChatCommandProcessor : IChatProcessor
	{
		// Token: 0x06002752 RID: 10066 RVA: 0x004946EC File Offset: 0x004928EC
		public ChatCommandProcessor AddCommand<T>() where T : IChatCommand, new()
		{
			ChatCommandAttribute cacheableAttribute = AttributeUtilities.GetCacheableAttribute<T, ChatCommandAttribute>();
			string commandKey = "ChatCommand." + cacheableAttribute.Name;
			ChatCommandId chatCommandId = ChatCommandId.FromType<T>();
			this._commands[chatCommandId] = ((default(T) == null) ? Activator.CreateInstance<T>() : default(T));
			if (Language.Exists(commandKey))
			{
				this._localizedCommands.Add(Language.GetText(commandKey), chatCommandId);
			}
			else
			{
				commandKey += "_";
				LocalizedText[] array = Language.FindAll((string key, LocalizedText text) => key.StartsWith(commandKey));
				foreach (LocalizedText key2 in array)
				{
					this._localizedCommands.Add(key2, chatCommandId);
				}
			}
			return this;
		}

		// Token: 0x06002753 RID: 10067 RVA: 0x004947D0 File Offset: 0x004929D0
		public ChatCommandProcessor AddDefaultCommand<T>() where T : IChatCommand, new()
		{
			this.AddCommand<T>();
			ChatCommandId key = ChatCommandId.FromType<T>();
			this._defaultCommand = this._commands[key];
			return this;
		}

		// Token: 0x06002754 RID: 10068 RVA: 0x00494800 File Offset: 0x00492A00
		private static bool HasLocalizedCommand(ChatMessage message, LocalizedText command)
		{
			string text = message.Text.ToLower();
			string value = command.Value;
			return text.StartsWith(value) && (text.Length == value.Length || text[value.Length] == ' ');
		}

		// Token: 0x06002755 RID: 10069 RVA: 0x0049484C File Offset: 0x00492A4C
		private static string RemoveCommandPrefix(string messageText, LocalizedText command)
		{
			string value = command.Value;
			if (!messageText.StartsWith(value))
			{
				return "";
			}
			if (messageText.Length == value.Length)
			{
				return "";
			}
			if (messageText[value.Length] == ' ')
			{
				return messageText.Substring(value.Length + 1);
			}
			return "";
		}

		// Token: 0x06002756 RID: 10070 RVA: 0x004948A8 File Offset: 0x00492AA8
		public bool ProcessOutgoingMessage(ChatMessage message)
		{
			KeyValuePair<LocalizedText, ChatCommandId> keyValuePair = this._localizedCommands.FirstOrDefault((KeyValuePair<LocalizedText, ChatCommandId> pair) => ChatCommandProcessor.HasLocalizedCommand(message, pair.Key));
			ChatCommandId value = keyValuePair.Value;
			if (keyValuePair.Key != null)
			{
				message.SetCommand(value);
				message.Text = ChatCommandProcessor.RemoveCommandPrefix(message.Text, keyValuePair.Key);
				return true;
			}
			return false;
		}

		// Token: 0x06002757 RID: 10071 RVA: 0x0049491C File Offset: 0x00492B1C
		public bool ProcessReceivedMessage(ChatMessage message, int clientId)
		{
			if (CommandManager.HandleCommand(message.Text, new PlayerCommandCaller(Main.player[clientId])))
			{
				return true;
			}
			IChatCommand chatCommand;
			if (this._commands.TryGetValue(message.CommandId, out chatCommand))
			{
				chatCommand.ProcessMessage(message.Text, (byte)clientId);
				return true;
			}
			if (this._defaultCommand != null)
			{
				this._defaultCommand.ProcessMessage(message.Text, (byte)clientId);
				return true;
			}
			return false;
		}

		// Token: 0x040043B8 RID: 17336
		private Dictionary<LocalizedText, ChatCommandId> _localizedCommands = new Dictionary<LocalizedText, ChatCommandId>();

		// Token: 0x040043B9 RID: 17337
		private Dictionary<ChatCommandId, IChatCommand> _commands = new Dictionary<ChatCommandId, IChatCommand>();

		// Token: 0x040043BA RID: 17338
		private IChatCommand _defaultCommand;
	}
}
